package com.curve;

import com.opensymphony.xwork.ActionSupport;
import com.service.CurveServiceFactory;
import com.lordor.om.action.AuthorizationUtil;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * User: lyj
 * Date: 2011-11-30
 * Time: 17:08:17
 */
public class ListMyPointsAction extends ActionSupport {
    private String id;
    private String pointId;
    private UserCurve uc;
    private String daystr;
    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

    private ArrayList myPointList=new ArrayList();
    private int maxDayNum = 50;
    public String execute() throws Exception {
           uc= CurveServiceFactory.getCurveService().findUserCurveById(id);
           List tempPointList= CurveServiceFactory.getCurveService().findMyCurvePoints(AuthorizationUtil.getAccount(),id);
           Map tempMap=new HashMap();
        for (int i = 0; i < tempPointList.size(); i++) {
              CurvePoint cp= (CurvePoint) tempPointList.get(i);
            tempMap.put(sdf.format(cp.getPointDate()),cp);

        }
        Calendar startCal=Calendar.getInstance();
        for (int i = 0; i < maxDayNum; i++) {
             CurvePoint tempPoint = new CurvePoint();
            GregorianCalendar tempCal = (new GregorianCalendar(startCal.get(Calendar.YEAR), startCal.get(Calendar.MONTH), i));
            tempPoint.setPointDate(tempCal.getTime());
            String key=sdf.format(tempCal.getTime());
            Object point = tempMap.get(key);
            if (point !=null) {
                myPointList.add(point);
            }else{
                myPointList.add(tempPoint);
            }

        }
        return SUCCESS;
    }

    public String addPoint() throws Exception {
//        uc= CurveServiceFactory.getCurveService().findUserCurveById(id);
        Date pointDay=sdf.parse(daystr);
        CurveServiceFactory.getCurveService().addCurvePoint(id,pointDay);
        return "add";
    }

    public String removePoint() throws Exception {
//        uc= CurveServiceFactory.getCurveService().findUserCurveById(id);
        CurveServiceFactory.getCurveService().removeCurvePoint(id,pointId);
        return "remove";
    }

    public UserCurve getUc() {
        return uc;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    public String getDaystr() {
        return daystr;
    }

    public void setDaystr(String daystr) {
        this.daystr = daystr;
    }


    public String getPointId() {
        return pointId;
    }

    public void setPointId(String pointId) {
        this.pointId = pointId;
    }


    public ArrayList getMyPointList() {
        return myPointList;
    }
}
